﻿using System;
using System.Collections.Generic;
using Lari.Framework.Exceptions;

//using Lari.Framework.Exceptions.Logging;

namespace Lari.Services.Utility
{
    public static class ErrorHandler //: IErrorHandler
    {
        // ErrorHandlingBehaviorAttribute _attribute;
        // IExceptionToFaultConverter _converter;

        //public ErrorHandler(ErrorHandlingBehaviorAttribute attribute)
        //{
        //    try
        //    {
        //        _attribute = attribute;
        //        if (_attribute.ExceptionToFaultConverter != null)
        //            _converter = (IExceptionToFaultConverter)Activator.CreateInstance(_attribute.ExceptionToFaultConverter);
        //    }
        //    catch(Exception e) 
        //    {
        //        ProcessException(MessageCode.Generic.SYSTEM, e, "Lari Framework");

        //    }

        //}

        //public bool HandleError(Exception error) 
        //{
        //    return true;
        //}

        //public void ProvideFault(Exception error, MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        //{
        //    //If it's a FaultException already, then we have nothing to do
        //    if (error is FaultException<LariFault>)
        //    {
        //        return;
        //    }
        //    try
        //    {
        //        //it could be a business exception by chance

        //        FaultException<LariFault> newFault = _converter.ToFaultException(MessageCode.Generic.WCF_GENERAL, error, false);
        //        MessageFault faultMessage = newFault.CreateMessageFault();
        //        fault = System.ServiceModel.Channels.Message.CreateMessage(version, faultMessage, newFault.Action);
        //    }
        //    catch
        //    {

        //        //ErrorHandler.HandleWCFWaring(MessageCode.Generic.SYSTEM, exc);
        //    }
        //}

        public static void ProcessException(string errorMessage, Exception ex, string moduleName,
                                            IDictionary<string, object> extentedProperties = null)
        {
            bool isLogingRequired = true;
           

            if (ex is LariException)
            {
                var ex2 = ex as LariException;
                //if (ex2.Id != 0)
                //    tempErrorcode = ex2.Id;
                isLogingRequired = ex2.IsLogRequired;
            }


            //if (!(ex is FaultException<LariFault> || ex.GetBaseException() is FaultException<LariFault>))
            //{
                if (isLogingRequired)
                    LogServiceHelper.WriteErrorLog(errorMessage, ex, moduleName);
            //}
            //if (wcf)
            //    throw new ServiceFaultProvider().ToFaultException(errorCode, ex, true);
        }

        /*
        public static void HandelNonWCFError(int errorCode, Exception ex, string moduleName, IDictionary<string, object> extentedProperties = null)
        {
            bool isLogingRequired = true;
            ex.Data.Add("SOURCE", moduleName);
            if (ex is LariException)
            {
                LariException ex2 = ex as LariException;

                ex.Data.Add("EVENTID", ex2.Id);
                isLogingRequired = ex2.IsLogRequired;
            }
            else
            {
                ex.Data.Add("EVENTID", errorCode);
            }

            if (!(ex is FaultException<LariFault> || ex.GetBaseException() is FaultException<LariFault>))
            {
                //if (isLogingRequired)
                    //LogServiceHelper.WriteErrorLog(ex, extentedProperties);
            }

            


        }

        public static void HandleWCFWaring(int errorCode, Exception ex)
        {
            ex.Data.Add("SOURCE", "WCF Warning");
            if (ex is LariException)
            {
                LariException ex2 = ex as LariException;

                ex.Data.Add("EVENTID", ex2.Id);
            }
            else
            {
                ex.Data.Add("EVENTID", errorCode);
            }


            if (!(ex is FaultException<LariFault> || ex.GetBaseException() is FaultException<LariFault>))
            {
                //LogServiceHelper.WriteInfoLog(ex);
            }
            
            
        }*/
    }
}